package com.situ.feemanagement.service.impl;

import com.situ.feemanagement.mapper.MedicineMapper;
import com.situ.feemanagement.mapper.OrderMapper;
import com.situ.feemanagement.mapper.RestockingMapper;
import com.situ.feemanagement.pojo.Medicine;
import com.situ.feemanagement.pojo.Restocking;
import com.situ.feemanagement.pojo.vo.EarningsVO;
import com.situ.feemanagement.pojo.vo.MedicineIdVO;
import com.situ.feemanagement.service.IEchartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EchartsServiceImpl implements IEchartsService {

    @Autowired
    private MedicineMapper medicineMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestockingMapper restockingMapper;

    @Override
    public List<EarningsVO> selectAll() {
        List<EarningsVO> list = new ArrayList<>();
        List<MedicineIdVO> medicineIdList = medicineMapper.selectIds();
        for (MedicineIdVO medicineId : medicineIdList) {
            Medicine medicine = medicineMapper.selectById(medicineId.getId());
            String name = medicine.getName();
            Integer number = orderMapper.selectByMedicineId(medicineId.getId());
            if (number == null) {
                number = 0;
            }
            double allSellPrice = number * medicine.getSellPrice();
            Restocking restocking = restockingMapper.selectByMedicineId(medicineId.getId());
            double allPurchasePrice = number * restocking.getPurchasePrice();
            double value = allSellPrice - allPurchasePrice;
            EarningsVO earningsVO = new EarningsVO(name,value);
            list.add(earningsVO);
        }
        return list;
    }
}
